package com.netsdk.alarm;

import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.NetSDKLib.LLong;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import java.util.Vector;

public class AlarmDataCallback implements NetSDKLib.fMessCallBack {

    private final Vector<AlarmEventInfo> data = new Vector<>();

    @Override
    public boolean invoke(int lCommand, LLong lLoginID, Pointer pStuEvent, int dwBufLen, String strDeviceIP, NativeLong nDevicePort, Pointer dwUser) {
        // 处理各种类型的报警
        switch (lCommand) {
            case NetSDKLib.NET_ALARM_ALARM_EX:
            case NetSDKLib.NET_MOTION_ALARM_EX:
            case NetSDKLib.NET_VIDEOLOST_ALARM_EX:
            case NetSDKLib.NET_SHELTER_ALARM_EX:
            case NetSDKLib.NET_DISKFULL_ALARM_EX:
            case NetSDKLib.NET_DISKERROR_ALARM_EX: {
                byte[] alarm = new byte[dwBufLen];
                pStuEvent.read(0, alarm, 0, dwBufLen);
                // 根据报警数据的状态，添加或移除报警事件
                for (int i = 0; i < dwBufLen; i++) {
                    if (alarm[i] == 1) { // 报警开始
                        AlarmEventInfo alarmEventInfo = new AlarmEventInfo(i, lCommand, AlarmStatus.ALARM_START);
                        if (!data.contains(alarmEventInfo)) {
                            data.add(alarmEventInfo);
                            // 可以在这里通知服务器其他模块处理报警信息
                        }
                    } else { // 报警结束
                        AlarmEventInfo alarmEventInfo = new AlarmEventInfo(i, lCommand, AlarmStatus.ALARM_STOP);
                        if (data.remove(alarmEventInfo)) {
                            // 可以在这里通知服务器其他模块处理报警结束信息
                        }
                    }
                }
                break;
            }
            default:
                break;
        }
        return true;
    }
}
